package com.jk.dmshopprovider.service;

import com.jk.dmcommon.entity.ShopEntity;
import com.jk.dmcommon.entity.TicketEntity;
import com.jk.dmcommon.LybEntity.UserEntity;
import com.jk.dmcommon.entity.YeWuEntity;
import com.jk.dmshopprovider.dao.ShopDao;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;


import javax.annotation.Resource;
import java.util.List;

/**
 * @author 赵优秀
 * @version 1.0.0
 * @ClassName ShopService.java
 * @Description TODO
 * @createTime 2021年09月09日 16:53:00
 */
@Service
public class ShopService {
    @Resource
    private ShopDao shopDao;
    @Autowired
    private RedisTemplate  redisTemplate;
    /**
     * @title 查询活动介绍
     * @description
     * @author 刘卧龙
     * @updateTime 2021/9/10 10:58
     * @throws
     */
    public ShopEntity findShop(Integer goodsId) {
        return shopDao.findShop(goodsId);
    }
    /**
     * @title 购买时所需优惠券
     * @description
     * @author 赵优秀
     * @updateTime 2021/9/10 11:00
     * @throws
     */
    public List<TicketEntity> getCoupon() {
        UserEntity user = (UserEntity) redisTemplate.opsForHash().get("dmLoginInfo","loginInfo");
        return shopDao.getCoupon(user.getUserId());
    }

    public YeWuEntity buy(YeWuEntity yeWuEntity) {
        redisTemplate.opsForHash().put("buy","1",yeWuEntity);
        return null;
    }

    public Object affirmBuy(YeWuEntity yeWuEntity) {
        redisTemplate.opsForHash().put("affirmBuy","1",yeWuEntity);
        return null;
    }

    //座位存redis
    public void zuowei(String site) {
        YeWuEntity buy = (YeWuEntity) redisTemplate.opsForHash().get("buy", "1");
        redisTemplate.opsForHash().put("zuowei","1",buy);
    }
    //wl生成座位
    public String createSite(Integer id) {
        String dmsite = (String) redisTemplate.opsForHash().get("dmsite", "site"+id);
        return dmsite;
    }
    //提交选座
    @Synchronized
    public String subSite(String siteArr,Integer id) {
        //redis取出数组
        String redisSite = (String) redisTemplate.opsForHash().get("dmsite", "site"+id);
        List<int[][]> redisList = JSONArray.parseArray(redisSite, int[][].class);
        //redis集合转成数组
        int[][][] redisArr = new int[redisList.size()][][];
        for (int i = 0;i<redisList.size();i++){
            redisArr[i]=redisList.get(i);
        }

        //前台json字符串转集合
        List<int[]> list = JSONArray.parseArray(siteArr, int[].class);
        //判断座位是否被占用
        for (int[] arr: list) {
            int x = arr[0];
            int y = arr[1];
            if(redisArr[x][y][0]==1){
                return "201";
            }
        }
        //没被占，修改redis座位信息
        for (int[] arr: list) {
            int x = arr[0];
            int y = arr[1];
            redisArr[x][y][0]=1;
        }
        //新的座位信息存入redis
        String newSite = JSONArray.toJSONString(redisArr);
        redisTemplate.opsForHash().put("dmsite", "site"+id,newSite);
        return "200";
    }
}
